/* ************************************************************************

   Copyright:

   License:

   Authors: SALMON Charles

************************************************************************ */

/* ************************************************************************

#require(bip.thirdParty.jsonrpc)

************************************************************************ */

/**
 * The rpc manager will create instance of buisiness logic component
 */
qx.Class.define("bip.rpc.RpcManager",
{
  extend: qx.core.Object,

  type: "singleton",

  /*
  *****************************************************************************
   EVENTS
  *****************************************************************************
  */
  events :
  {


  },

  /*
  *****************************************************************************
   CONSTRUCTOR
  *****************************************************************************
  */

  /**
   * Connect to the JSON-RPC server
   *
   */
   construct: function()
  {
    this.base(arguments);

    this.__debug = false;
    this.__isCreated = false;
    this.__jsonUrl = "$[JSON-RPC-URI]$";
   
    //Try to connect to the rpc servlet
    try {
      this.info("Json URL:"+this.__jsonUrl);
      this.__jsonrpc = new JSONRpcClient(this.__jsonUrl);
      //dealing with session timeout
      bip.thirdParty.jsonrpc.setManager(this);
      /*bip.thirdParty.jsonrpc.sessionTimer.setInterval(1000*60*20);  // Recall the user that session is about to expire every 20 minutes
      bip.thirdParty.jsonrpc.sessionTimer.start();
      bip.thirdParty.jsonrpc.sessionTimer.addListener("interval", function() {
        this.info("Session refresh");
        this.__jsonrpc.Session.refreshSession();
      },this );*/
      this.__isCreated = true;
    } catch(e) {
      this.fatalError(e,"Could not create json rpc ");
    }
  },

  /*
  *****************************************************************************
   MEMBERS
  *****************************************************************************
  */
  members:
  {
    /**
     * Cancel a request that have been dispatched
     * @param reqId {Integer} The request id
     */
    cancelRequest: function(reqId) {
      JSONRpcClient.cancelRequest(reqId);
    },

    /**
     * Close the session
     */
    closeSession: function() {
      this.__jsonrpc.Session.invalidateSession();
    },

    /**
     * Get the name of the session user
     */
    getUserName: function() {
      return this.__jsonrpc.Session.getUsername();
    },

    /**
     * return true is user authenticate as a BIP_USER
     */
    isBipUser: function() {
      return this.__jsonrpc.Session.isBipUser();
    },

    /**
     * return true is user authenticate as a BIP_AUTHOR
     */
    isBipAuthor: function() {
      return this.__jsonrpc.Session.isBipAuthor();
    },

    /**
     * Return the Json Rpc object
     */
    getJsonRpc: function() {
      return this.__jsonrpc;
    },

    /**
     * Return a new object that is mapped to an existing instance on server side
     * @param objectId {Integer}
     */
    newProxyObject: function(objectId) {
      return new JSONRpcClient(this.__jsonUrl,null,null,objectId);
    },

    /*
    *****************************************************************************
     ERRORS HANDLING
    *****************************************************************************
    */
    /**
     * Will display a fatal error window
     *
     * @param error {Object} The error object
     * @param title {String} The window title
     */
    fatalError: function(error,title) {
      var errorMsg = "";
      if (error.msg != null) { errorMsg += error.msg +" ";}
      if (error.message != null) { errorMsg += error.message +" ";}
      alert("FATAL ERROR !\n "+errorMsg);
    }
  }

});
